<?php

namespace app\api\controller;

use app\common\exception\BusinessException;
use think\response\Json;

class Paper extends Base
{

    /**
     * 试卷列表
     * @throws
     */
    public function getList(): Json
    {
        //外部参数
        $param = $this->request->param();

        // 数据验证
        if (empty($param['course_id']) && empty($param['category_id'])) {
            throw new BusinessException('请求参数异常', 0);
        }

        // 查询数据
        $list = app(\app\api\logic\Paper::class)->getList($param);

        // 返回数据
        return $this->response($list);
    }


    /**
     * 分类列表
     * @throws
     */
    public function categoryList(): Json
    {
        //外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'course_id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'course_id.require' => '课程编号不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 查询数据
        $list = app(\app\api\logic\Paper::class)->categoryList($param);

        // 返回数据
        return $this->response($list);
    }

    /**
     * 分类详情
     * @throws
     */
    public function categoryDetail(): Json
    {
        //外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '分类编号不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 查询数据
        $info = app(\app\api\logic\Paper::class)->categoryDetail($param);

        // 返回数据
        return $this->response($info);
    }
}